/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xuexiang.xuidemo.slice.imageview.preview.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.ImageViewInfo;
import com.xuexiang.xuidemo.provider.PageVideoSliderAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * RecycleView视频滑动
 *
 * @since 2021-04-08
 */
public class BannerVideoPagerSlice extends AbilitySlice implements PageVideoSliderAdapter.OnItemClickListener {
    ArrayList<String> pages = new ArrayList<>();
    List<ImageViewInfo> videoImageList = new ArrayList<>();
    PageSlider pageSlider, pageVideoSlider;
    Text tv_count;
    private String position;

    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_banner_pager);
        initView();
        initData();
        if (intent != null) {
            position = intent.getStringParam("position");
            tv_count.setText(position + "" + "/" + videoImageList.size() + "");
        }
        pageVideoSlider.setCurrentPage(Integer.parseInt(position) - 1);
    }

    private void initView() {
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageVideoSlider = (PageSlider) findComponentById(ResourceTable.Id_page_video_slider);
        pageSlider.setVisibility(Component.HIDE);
        pageVideoSlider.setVisibility(Component.VISIBLE);
        tv_count = (Text) findComponentById(ResourceTable.Id_tv_count);
        pageVideoSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosPixles) {
            }

            @Override
            public void onPageSlideStateChanged(int state) {
            }

            @Override
            public void onPageChosen(int itemPos) {
                tv_count.setText(itemPos + 1 + "" + "/" + videoImageList.size() + "");
            }
        });
    }

    private void initData() {
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-05-25/b146e104069c2bd0590bb919269193c4/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53.mp4",
            "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-07-18/80d08ce1a84adfbaed5c7067b73d19ed.mp4",
            "https://pic.vjshi.com/2017-07-18/80d08ce1a84adfbaed5c7067b73d19ed/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2018-06-07/cf673556cce54ab9cf4633fd7d9d0d46.mp4",
            "https://pic.vjshi.com/2018-06-06/caa296729c8e6e41e6aff2aadf4feff3/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2018-06-07/cf673556cce54ab9cf4633fd7d9d0d46.mp4",
            "https://pic.vjshi.com/2018-06-06/caa296729c8e6e41e6aff2aadf4feff3/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424.mp4",
            "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));

        for (int i = 0; i < videoImageList.size(); i++) {
            pages.add(videoImageList.get(i).getUrl());
        }
        PageVideoSliderAdapter adapter = new PageVideoSliderAdapter(pages, this, this, this);
        pageVideoSlider.setProvider(adapter);
    }

    @Override
    public void onItemClick(Component component, int position) {
        String mUrl = videoImageList.get(position).getVideoUrl();
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withDeviceId("")
            .withBundleName(getBundleName())
            .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.VideoPlayAbility")
            .build();
        intent.setParam("url", mUrl);
        intent.setOperation(operation);
        startAbility(intent);
    }
}
